package com.example.cardserver.service.impl;

import com.example.cardserver.entity.User;
import com.example.cardserver.mapper.UserMapper;
import com.example.cardserver.service.IUserService;
import com.example.cardserver.service.ex.UserException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements IUserService {
    @Resource
    public UserMapper userMapper;
    
    @Override
    public void reg(User user) {
        String phone = user.getPhone();
        User result = userMapper.findByPhone(phone);

        if(result!=null){
            throw new UserException("手机号已注册");
        }
        String code = user.getCode();
        result = userMapper.findByCode(code);
        if(result!=null){
            throw new UserException("身份证号已存在");
        }
        
//        String password = user.getPassword();
//        user.setPassword(password);
        Integer row = userMapper.insert(user);
        if(row!=1){
            throw new UserException("注册失败");
        }
    }
    

    @Override
    public User login(String code, String password) {
        User result = userMapper.findByCode(code);
        if(result==null){
            throw new UserException("用户未注册");
        }

        if(!result.getPassword().equals(password)){
            throw new UserException("用户密码错误");
        }
        
        ///???????  result?user
        return result;
    }

    @Override
    public void changePassword(Integer id, String phone, String oldPassword, String newPassword) {
        User result = userMapper.findById(id);
        if(result==null){
            throw new UserException("用户不存在");
        }
        if(!oldPassword.equals(result.getPassword())){
            throw new UserException("原密码错误");
        }
        Integer row = userMapper.updatePasswordById(id, newPassword);
        if(row != 1){
            throw new UserException("更新时出现未知错误");
        }
    }

    @Override
    public User getById(Integer id) {
        User result = userMapper.findById(id);
        if(result==null){
            throw new UserException("用户不存在");
        }
        User user = new User();
        user.setPhone(result.getPhone());
        user.setAvatar(result.getAvatar());
        return user;
    }

    @Override
    public User getByPhone(String phone) {
        User result = userMapper.findByPhone(phone);
        if(result==null){
            throw new UserException("用户不存在");
        }
        User user = new User();
        user.setPhone(result.getPhone());
        user.setAvatar(result.getAvatar());
        user.setCommentCode(result.getCommentCode());
        user.setBalance(result.getBalance());
        return user;
    }

    @Override
    public void changeAvatar(Integer id, String avatar) {
        User result = userMapper.findById(id);
        if(result==null){
            throw new UserException("用户不存在");
        }
        Integer row = userMapper.updateAvatarById(id, avatar);
        if(row != 1){
            throw new UserException("更新时出现异常");
        }
    }

    @Override
    public void getBonus(String bonus) {
        User user = userMapper.findByComment(bonus);
        userMapper.getBonus(user.getId());
    }
}
